﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class VABPOS
    {
        public void CreateKSLEIST(string ITXDBVERSION, DataSet db)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            DataTable tbABP = db.Tables["ICC_PDC"];
            double QTY = 0;
            DataRow dwABF = null;
            var qPDC = from DataRow row in tbABP.AsEnumerable()
                       where row["PROCESSED"].ToString() == " "
                       select row;
            foreach (DataRow dwPDC in qPDC)
            {
                //根据此时的情况，判断ABFOLGE
                SResult = INTEXDBC.QueryABFOLGE(dwPDC, 3);
                if (SResult.Tables[0].Rows.Count == 0)
                    continue;
                dwABF = SResult.Tables[0].Rows[0];
                double ABFIMG = Convert.ToDouble(dwABF["ABFIMG"].ToString()) / 1000;
                double ABFSOLLMG = Convert.ToDouble(dwABF["ABFSOLLMG"].ToString()) / 1000;
                string ABFISTARTDAT = dwABF["ABFISTARTDAT"].ToString();//开始日期
                string PDCFLAG = dwPDC["PDCFLAG"].ToString();//开始时间 
                //ABFRAFOLGE=:ABFRAFOLGE,ABFRUECKBEN=:ABFRUECKBEN,ABFRUECKDAT=:ABFRUECKDAT,ABFRUECKZT=:ABFRUECKZT,ABFIMNRKRS=:ABFIMNRKRS,ABFIMLFDNR=:ABFIMLFDNR,ABFIMPOS=:ABFIMPOS
                //,ABFISTARTDAT=:ABFISTARTDAT,ABFISTARTZT=:ABFISTARTZT,ABFSTATUS=:ABFSTATUS,ABFIENDDAT=:ABFIENDDAT,ABFIENDZT=:ABFIENDZT
                dwABF["ABFRAFOLGE"] = dwPDC["ABFAFOLGE"];
                dwABF["ABFRUECKBEN"] = dwPDC["NEWUSER"];
                dwABF["ABFRUECKDAT"] = dwPDC["PDCDATE"];
                dwABF["ABFRUECKZT"] = dwPDC["PDCTIMES"];
                dwABF["ABFIMNRKRS"] = dwPDC["ABFIMNRKRS"];
                dwABF["ABFIMLFDNR"] = dwPDC["ABFIMLFDNR"];
                dwABF["ABFIMPOS"] = dwPDC["ABFIMPOS"];
                if (ABFISTARTDAT.Trim() == "0")
                {
                    dwABF["ABFISTARTDAT"] = dwPDC["PDCDATE"];
                    dwABF["ABFISTARTZT"] = dwPDC["PDCTIMES"];
                }
                QTY = Convert.ToDouble(dwPDC["QTY"].ToString());
                dwABF["ABFIMG"] = (ABFIMG + QTY) * 1000;
                string KSLERST = "N";
                if (QTY == 0 && ABFIMG == 0
                    || PDCFLAG == "S")//开始
                {
                    dwABF["ABFISTARTDAT"] = dwPDC["PDCDATE"];
                    dwABF["ABFISTARTZT"] = dwPDC["PDCTIMES"];
                    KSLERST = "J";
                }
                if (PDCFLAG == "T")//结束
                {
                    dwABF["ABFSTATUS"] = "R";
                }
                if (QTY + ABFIMG >= ABFSOLLMG)//结束
                {
                    dwABF["ABFSTATUS"] = "R";
                    dwABF["ABFIENDDAT"] = dwPDC["PDCDATE"];
                    dwABF["ABFIENDZT"] = dwPDC["PDCTIMET"];
                }
                if (dwABF["ABFRUECKZT"].ToString().Trim() == "")
                {
                    dwABF["ABFRUECKZT"] = "0"; 
                }
                INTEXDBC.UpdateABFOLGE(dwABF, 5);
                dwPDC["PROCESSED"] = "Y";
                string _date = "", _time = "", _stime = "";
                Helper.GetSTime(ref _date, ref _time, ref _stime);
                dwPDC["CHGUSER"] = dwPDC["NEWUSER"];
                dwPDC["CHGDATE"] = _date;
                dwPDC["CHGTIME"] = _stime;
                INTEXDBC.UpdateICC_PDC(dwPDC,4, app.company);
                if (QTY == 0)
                {
                    continue;//仅仅更新ABFOLGE
                }
                try
                {
                    DataRow dwKSL = INTEXDBC.Convert2KSLEIST(null, ITXDBVERSION, false);
                    #region====KSLEIST======
                    int idx = dwPDC["ABAID"].ToString().IndexOf('.');

                    string KSLAENDZL = dwPDC["ABAID"].ToString().Substring(idx + 1).Trim();
                    dwKSL["KSLMANDANT"] = dwPDC["ABFMANDANT"];
                    dwKSL["KSLAUFKRS"] = dwPDC["ABFAUFKRS"];
                    dwKSL["KSLAUFLFD"] = dwPDC["ABFAUFLFD"];
                    dwKSL["KSLAUFPOS"] = dwPDC["ABFAUFPOS"];
                    dwKSL["KSLAFOLGE"] = dwPDC["ABFAFOLGE"];
                    dwKSL["KSLAENDZL"] = KSLAENDZL;
                    //KSLMDAT,KSLMGKG,KSLPERSNR
                    dwKSL["KSLMDAT"] = dwPDC["PDCDATE"];
                    dwKSL["KSLSCHICHT"] = dwPDC["PDCSHIFT"];
                    dwKSL["KSLMZTV"] = dwPDC["PDCTIMES"];//开始
                    if (dwKSL["KSLMZTV"].ToString() == "")
                    {
                        dwKSL["KSLMZTV"] = 0;
                    }
                    dwKSL["KSLMZTB"] = dwPDC["PDCTIMET"];//结束
                    dwKSL["KSLPERSNR"] = Helper.Left(dwPDC["PDCSTAFF"].ToString().PadRight(8), 8);
                    dwKSL["KSLMBENID"] = dwPDC["NEWUSER"];
                    dwKSL["KSLMPROG"] = Helpers.Helper.Left(dwPDC["JP11"].ToString().PadRight(8), 8);
                    dwKSL["KSLMAFOLGE"] = dwPDC["ABFAFOLGE"];
                    dwKSL["KSLMCODE"] = "   ";
                    dwKSL["KSLSA"] = "1";
                    dwKSL["KSLHERK"] = "P";
                    dwKSL["KSLVST"] = "N";
                    dwKSL["KSLIKOSTBW"] = " ";
                    dwKSL["KSLKAP"] = " ";
                    dwKSL["KSLDIV"] = " ";
                    dwKSL["KSLVERT"] = "N";
                    dwKSL["KSLMELDKZ"] = "1";
                    dwKSL["KSLERST"] = KSLERST;//TBA
                    dwKSL["KSLAUFTYP"] = "V";
                    dwKSL["KSLAUFART"] = dwPDC["ABKAUFART"];
                    dwKSL["KSLTP"] = dwPDC["MKFTP"];
                    dwKSL["KSLFOLGEV"] = "00";
                    dwKSL["KSLFOLGEB"] = "00";
                    dwKSL["KSLSTKNR"] = "            ";
                    dwKSL["KSLAGANG"] = dwPDC["ABFAGANG"];
                    dwKSL["KSLPLANKZ"] = " ";
                    dwKSL["KSLFIX"] = dwPDC["ABFFIX"];
                    dwKSL["KSLMANUELKZ"] = dwPDC["ABFMANUELKZ"];
                    dwKSL["KSLANTERLS"] = "0";
                    dwKSL["KSLERSTKZ"] = " ";
                    dwKSL["KSLZTAMMELD"] = " ";
                    dwKSL["KSLKUNDNR"] = "00000000";
                    dwKSL["KSLWERK"] = "   ";
                    dwKSL["KSLABT"] = "   ";

                    dwKSL["KSLWAKZ"] = dwPDC["ABPWAKZ"];
                    dwKSL["KSLART"] = dwPDC["ABPART"];
                    dwKSL["KSLVAR"] = dwPDC["ABPVAR"];
                    dwKSL["KSLDESS"] = dwPDC["ABPDESS"];
                    dwKSL["KSLCOLORIT"] = dwPDC["ABPCOLORIT"];
                    dwKSL["KSLBESCHICH"] = dwPDC["ABPBESCHICH"];
                    dwKSL["KSLFARBE"] = dwPDC["ABPFARBE"];
                    dwKSL["KSLZUSTAND"] = "    ";
                    dwKSL["KSLPROGRP"] = "   ";
                    dwKSL["KSLARTGRP"] = "   ";
                    dwKSL["KSLKAAGRA"] = "     ";
                    dwKSL["KSLKAAGRP"] = "  ";
                    dwKSL["KSLKZINLEXP"] = " ";
                    dwKSL["KSLKPFL"] = " ";
                    dwKSL["KSLME"] = dwPDC["ABFME"];
                    dwKSL["KSLMGANZ"] = "0";
                    dwKSL["KSLMGMTR"] = "0";
                    dwKSL["KSLMGQM"] = "0";
                    dwKSL["KSLMGKG"] = QTY * 1000;
                    dwKSL["KSLSAKSTKRS"] = dwPDC["ABFKSTKRS"];
                    dwKSL["KSLSAKST"] = dwPDC["ABFKST"];
                    dwKSL["KSLSMNRKRS"] = dwPDC["ABFIMNRKRS"];
                    dwKSL["KSLSMLFDNR"] = dwPDC["ABFIMLFDNR"];
                    dwKSL["KSLSMPOS"] = dwPDC["ABFIMPOS"];

                    dwKSL["KSLSEVARHNR"] = dwPDC["ABFEVARHNR"];
                    dwKSL["KSLSEVARUNR"] = dwPDC["ABFEVARHNR"];
                    dwKSL["KSLSMZT"] = "0";
                    dwKSL["KSLSPZT"] = "0";
                    dwKSL["KSLSRML"] = "0";
                    dwKSL["KSLSRMS"] = "0";
                    dwKSL["KSLSRPZ"] = "0";
                    dwKSL["KSLGKSTKRS"] = dwPDC["ABFKSTKRS"];
                    dwKSL["KSLGKST"] = dwPDC["ABFKST"];
                    dwKSL["KSLIAKSTKRS"] = dwPDC["ABFKSTKRS"];
                    dwKSL["KSLIAKST"] = dwPDC["ABFKST"];
                    dwKSL["KSLIMNRKRS"] = dwPDC["ABFIMNRKRS"];
                    dwKSL["KSLIMLFDNR"] = dwPDC["ABFIMLFDNR"];
                    dwKSL["KSLIMPOS"] = dwPDC["ABFIMPOS"];
                    dwKSL["KSLIEVARHNR"] = dwPDC["ABFEVARHNR"];
                    dwKSL["KSLIEVARUNR"] = dwPDC["ABFEVARHNR"];
                    dwKSL["KSLIMZT"] = dwPDC["PDCTIME"];
                    dwKSL["KSLIPZT"] = "0";
                    dwKSL["KSLIRML"] = "0";
                    dwKSL["KSLIRMS"] = "0";
                    dwKSL["KSLIRPZ"] = "0";
                    dwKSL["KSLLIEFNR"] = "        ";
                    dwKSL["KSLRENR"] = "          ";
                    dwKSL["KSLREPOS"] = "000";
                    dwKSL["KSLLSKOST"] = "0";
                    dwKSL["KSLLIKOST"] = "0";
                    dwKSL["KSLLIKOSTI"] = "0";
                    dwKSL["KSLRSKOST"] = "0";
                    dwKSL["KSLRIKOST"] = "0";
                    dwKSL["KSLRIKOSTI"] = "0";
                    dwKSL["KSLRSKOSTF"] = "0";
                    dwKSL["KSLRIKOSTF"] = "0";
                    dwKSL["KSLRIKOSTIF"] = "0";
                    dwKSL["KSLSQKOSTM1"] = "0";
                    dwKSL["KSLSQKOSTM2"] = "0";
                    dwKSL["KSLSQKOSTM3"] = "0";
                    dwKSL["KSLSQKOSTM4"] = "0";
                    dwKSL["KSLSQKOSTM5"] = "0";
                    dwKSL["KSLIQKOSTM1"] = "0";
                    dwKSL["KSLIQKOSTM2"] = "0";
                    dwKSL["KSLIQKOSTM3"] = "0";
                    dwKSL["KSLIQKOSTM4"] = "0";
                    dwKSL["KSLIQKOSTM5"] = "0";
                    dwKSL["KSLIQKOSTIM1"] = "0";
                    dwKSL["KSLIQKOSTIM2"] = "0";
                    dwKSL["KSLIQKOSTIM3"] = "0";
                    dwKSL["KSLIQKOSTIM4"] = "0";
                    dwKSL["KSLIQKOSTIM5"] = "0";
                    dwKSL["KSLSQKOSTP1"] = "0";
                    dwKSL["KSLSQKOSTP2"] = "0";
                    dwKSL["KSLSQKOSTP3"] = "0";
                    dwKSL["KSLSQKOSTP4"] = "0";
                    dwKSL["KSLSQKOSTP5"] = "0";
                    dwKSL["KSLIQKOSTP1"] = "0";
                    dwKSL["KSLIQKOSTP2"] = "0";
                    dwKSL["KSLIQKOSTP3"] = "0";
                    dwKSL["KSLIQKOSTP4"] = "0";
                    dwKSL["KSLIQKOSTP5"] = "0";
                    dwKSL["KSLIQKOSTIP1"] = "0";
                    dwKSL["KSLIQKOSTIP2"] = "0";
                    dwKSL["KSLIQKOSTIP3"] = "0";
                    dwKSL["KSLIQKOSTIP4"] = "0";
                    dwKSL["KSLIQKOSTIP5"] = "0";
                    dwKSL["KSLGSKOST1"] = "0";
                    dwKSL["KSLGSKOST2"] = "0";
                    dwKSL["KSLGSKOST3"] = "0";
                    dwKSL["KSLGSKOST4"] = "0";
                    dwKSL["KSLGSKOST5"] = "0";
                    dwKSL["KSLGIKOST1"] = "0";
                    dwKSL["KSLGIKOST2"] = "0";
                    dwKSL["KSLGIKOST3"] = "0";
                    dwKSL["KSLGIKOST4"] = "0";
                    dwKSL["KSLGIKOST5"] = "0";
                    dwKSL["KSLGIKOSTI1"] = "0";
                    dwKSL["KSLGIKOSTI2"] = "0";
                    dwKSL["KSLGIKOSTI3"] = "0";
                    dwKSL["KSLGIKOSTI4"] = "0";
                    dwKSL["KSLGIKOSTI5"] = "0";
                    dwKSL["KSLAENDZLST"] = "0";
                    dwKSL["KSLAENDZLSV"] = "0";
                    dwKSL["KSLENDKZ"] = " ";
                    dwKSL["KSLABG"] = " ";
                    dwKSL["KSLARBPL"] = "        ";
                    dwKSL["KSLLSTAR"] = "      ";
                    dwKSL["KSLLO"] = "  ";
                    dwKSL["KSLPAUFKRS"] = "  ";
                    dwKSL["KSLPAUFLFD"] = "000000";
                    dwKSL["KSLPAFOLGE"] = "000";
                    dwKSL["KSLFILLER"] = "0000000000000      *                                   ";
                    #endregion
                    INTEXDBC.SaveKSLEIST(dwKSL, app.company);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                }
            }
        }
        void DoITXPDCWork()
        {
            if (IsRun)
            {
                return;
            }
            IsRun = true;
            string path = "C:/IntexReport/Data/ITXPDC";
            ITXPDC(app.company, path);
        }
        public DataSet GetDBJob(string path)
        {
            DataSet SResult;
            DataSet SReturn = new DataSet();

            try
            {
                //SQLiteDatabase SQLiteDB;
                /*string dbConnection = path + "/" + app.company + ".s3db";
                SQLiteDB = new SQLiteDatabase(dbConnection);
                
                string sql = "SELECT* FROM ICC_PDC WHERE PROCESSED=' ' ORDER BY ABAID";
                SResult = SQLiteDB.GetDataTable(sql);*/
                SReturn = INTEXDBC.QueryICC_PDC(null, 11);
                /*
                foreach (DataRow row in SReturn.Tables[0].Rows)
                {
                    sql = "UPDATE ICC_PDC SET PROCESSED='Y' WHERE ABAID='" + row["ABAID"].ToString() + "'";
                    SQLiteDB.ExecuteNonQuery(sql);
                }*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public void ITXPDC(string ITXDBVERSION, string path)
        {
            try
            {

                while (true)
                {
                    DataSet SetJob = GetDBJob(path);
                    if (SetJob.Tables[0].Rows.Count == 0)
                    {
                        IsRun = false;
                        break;
                    }
                    CreateKSLEIST(ITXDBVERSION, SetJob);

                }
                EventDispatcher.Instance.RaiseStatus("ITXPDC完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                EventDispatcher.Instance.RaiseStatus("ITXPDC异常终止", StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                IsRun = false;
            }
        }
    }
}

